package com.echo.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import com.echo.fragment.WeekDayDataFragment;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by EchoGe on 2016/5/13.
 */
public class WeekDataAdapter extends FragmentPagerAdapter {

    private Calendar[] mDates;
    private List<String> mTitles=new ArrayList<>();

    private Fragment[] fragments;

    public WeekDataAdapter(FragmentManager fm, Calendar[] dates,List<String> titles){
        super(fm);
        this.mDates=dates;
        this.mTitles=titles;
        fragments=new Fragment[mDates.length];
    }

    public WeekDataAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        if(fragments[position]==null) {
            fragments[position] = new WeekDayDataFragment();
            ((WeekDayDataFragment)fragments[position]).setCalendar(mDates[position]);
        }
        return fragments[position];
    }

    public void refreshFragment(int position){
        ((WeekDayDataFragment)fragments[position]).update();
    }

    /*@Override
    public Object instantiateItem(ViewGroup container, int position){

    }*/

    @Override
    public int getCount() {
        return mDates.length;
    }

    public CharSequence getPageTitle(int position){return mTitles.get(position);}
}
